﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class ReceivingDAO
    {
        private ReceivingListDTO CreateReceivingFromEntity(PhieuNhap pn)
        {
            ReceivingListDTO receivingDto = new ReceivingListDTO
            {
                ReceivingID = pn.PhieuNhapID,
                ReceivingCode = pn.MaPhieuNhap,
                ReceivingDate = pn.NgayNhap ?? DateTime.Now,
                CreatedBy = pn.NguoiLap ?? 0,
                GrandTotal = float.Parse( pn.TongTien.ToString()),
                 Description = pn.GhiChu ?? string.Empty,
                 Status = pn.SuDung ?? false
            };

            using (BookStoreEntities ent = new BookStoreEntities())
            {
                NhanVien nv = ent.NhanViens.First(n => n.NhanVienID == pn.NguoiLap);
                receivingDto.CreatorName = nv.HoTen;
            }

            return receivingDto;
        }

        public int InsertReceiving(ReceivingListDTO receiDto)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    PhieuNhap pn = new PhieuNhap
                    {
                        MaPhieuNhap = receiDto.ReceivingCode,
                        NgayNhap = receiDto.ReceivingDate,
                        TongTien = receiDto.GrandTotal,
                        NguoiLap = receiDto.CreatedBy,
                        SuDung = receiDto.Status,
                        GhiChu = receiDto.Description
                    };

                    if (ent.PhieuNhaps.Count(t => t.PhieuNhapID != null) > 0)
                    {
                        pn.PhieuNhapID = ent.PhieuNhaps.Max(t => t.PhieuNhapID) + 1;
                    }
                    else
                    {
                        pn.PhieuNhapID = 1;
                    }

                    ent.AddToPhieuNhaps(pn);

                    DocSequenceDAO docSeqDAO = new DocSequenceDAO();
                    docSeqDAO.UpdateNextDocSequence(DocSequence.Receiving.ToString());

                    ent.SaveChanges();

                    return pn.PhieuNhapID;
                }
            }
            catch
            {
                return -1;
            }
        }

        public int InsertReceivingDetail(ReceivingDetailDTO receiDetail)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    ChiTietPhieuNhap ctpn = new ChiTietPhieuNhap
                    {
                        PhieuNhapID = receiDetail.ReceivingID,
                        SachID = receiDetail.BookID,
                        SoLuong = receiDetail.Quantity,
                        DonGia = receiDetail.UnitPrice
                    };

                    if (ent.ChiTietPhieuNhaps.Count(t => t.ChiTietID != null) > 0)
                    {
                        ctpn.ChiTietID = ent.ChiTietPhieuNhaps.Max(t => t.ChiTietID) + 1;
                    }
                    else
                    {
                        ctpn.ChiTietID = 1;
                    }

                    ent.AddToChiTietPhieuNhaps(ctpn);
                    ent.SaveChanges();

                    return ctpn.ChiTietID;
                }
            }
            catch
            {
                return -1;
            }
        }

        public int GetReceivingQuantity(int bookID, DateTime from, DateTime to)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                int quantity = 0;
                List<PhieuNhap> listReceive = ent.PhieuNhaps.Where(s => s.NgayNhap >= from && s.NgayNhap <= to).ToList();
                foreach (PhieuNhap pn in listReceive)
                {
                    quantity += ent.ChiTietPhieuNhaps.First(p => p.PhieuNhapID == pn.PhieuNhapID && p.SachID == bookID).SoLuong ?? 0;
                }

                return quantity;
            }
        }
    }
}
